﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

// Needed for Office and Excel integration
using Excel = Microsoft.Office.Interop.Excel;

namespace Lexalytics4Excel.Dialogs
{
    public partial class RangeSelector : Form
    {
        private Excel.Application _excel  = null;
        private Lexalytics4Excel.LexaScopeRibbon _ribbon = null;

        public RangeSelector(Excel.Application excel, LexaScopeRibbon ribbon)
        {
            InitializeComponent();
            _ribbon = ribbon;
            _excel = excel;
            refedit1._Excel = _excel;
        }

        private void RangeSelector_Load(object sender, EventArgs e)
        {
            refedit1.Focus();
        }

        private void SelectButton_Click(object sender, EventArgs e)
        {
            // Validate the user's input
            string DataRange = refedit1.Text;
            bool bValid = false;

            // Assume current workbook and worksheet
            string Workbook = _excel.ActiveWorkbook.Name;
            string Worksheet = ((Excel.Worksheet)_excel.ActiveSheet).Name;
            string SelColumn = "";
            if (DataRange.LastIndexOf("!")>-1)
            {
                SelColumn = DataRange.Substring(DataRange.LastIndexOf("!")+1);
                DataRange = DataRange.Remove(DataRange.LastIndexOf("!"));
                if (DataRange.LastIndexOf("]") > -1)
                {
                    Worksheet = DataRange.Substring(DataRange.LastIndexOf("]") + 1);
                    DataRange = DataRange.Remove(DataRange.LastIndexOf("]"));
                    if (DataRange.LastIndexOf("[") > -1)
                    {
                        Workbook = DataRange.Substring(DataRange.LastIndexOf("[")+1);
                        bValid = true;
                    }
                }
                else
                {
                    if (DataRange.LastIndexOf("[") == -1)
                    {
                        Worksheet = DataRange;
                        bValid = true;
                    }
                }
            }

            if (bValid)            
            {
                int keyRow = 1;
                // Determine the number of assets which could be added, display confirmation
                Excel.Workbook _wbook = (Excel.Workbook)_excel.Workbooks[Workbook];
                Excel.Worksheet _sheet = (Excel.Worksheet)_wbook.Worksheets[Worksheet];
                int nRows = 0;
                if (HeaderCheckbox.Checked)
                    keyRow++;
                Excel.Range _rng = (Excel.Range)(_sheet).Cells[keyRow, SelColumn];
                System.Diagnostics.Debug.Write(_rng.EntireColumn.Count.ToString());
                while (keyRow < _rng.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row)
                {
                    Excel.Range _rngCell = (Excel.Range)_sheet.Cells[keyRow, SelColumn];
                    if ((_rngCell.Value2 != null) && (_rngCell.Value2.ToString().Length > 0))
                        nRows++;
                    keyRow++;
                }
                
                if (MessageBox.Show(String.Format("This will add at most {0} items to your project (duplicates will be ignored).", nRows), "Confirm data selection", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                {
                    this.Hide();
                    _ribbon.ChangeRange(String.Format("[{0}]{1}!{2}", Workbook, Worksheet, SelColumn), HeaderCheckbox.Checked);
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Invalid range specified. Please reselect.", "Analyze Excel data", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void refedit1_CellChanged(object sender, EventArgs e)
        {
            SelectButton.Enabled = (refedit1.Text.Length > 0);
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
